package com.cabrida.livestar.main;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cabrida.livestar.R;
import com.cabrida.livestar.main.FansStarCircleLvAdapter.CircleLikeListener;
import com.cabrida.livestar.model.StarCircleData;

/**
 * 粉丝端星友圈
 * 
 * @author why
 */
public class FansStarCircleView extends LinearLayout implements OnClickListener {
	private Context mContext;
	private ImageView iv_back_bg;
	private TextView tv_title;
	private ListView listView;
	private FansStarCircleLvAdapter adapter;
	private ArrayList<StarCircleData> infoList=new ArrayList<StarCircleData>();

	public FansStarCircleView(Context context) {
		super(context);
		mContext = context;
		View view = LayoutInflater.from(context).inflate(
				R.layout.view_main_fans_starcircle, this);
		Init(view);
	}

	/**
	 * 初始化控件
	 */
	private void Init(View view) {
		iv_back_bg = (ImageView) findViewById(R.id.iv_back_bg);
		tv_title = (TextView) findViewById(R.id.tv_title);
		iv_back_bg.setVisibility(View.INVISIBLE);
		tv_title.setText(mContext.getString(R.string.text_star_circle));
		listView = (ListView) view.findViewById(R.id.listView);
		adapter = new FansStarCircleLvAdapter(mContext);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				StarCircleData data = (StarCircleData) arg0.getAdapter().getItem(arg2);
				Intent intent = new Intent();
				intent.setClass(mContext, FansCircleActivity.class);
				intent.putExtra("data", data);
				mContext.startActivity(intent);
			}
		});
		
		adapter.setOnCircleLikeListener(new CircleLikeListener() {
			
			@Override
			public void onLike(String cid) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.putExtra("cid", cid);
				intent.setAction(MainActivity.ACTION_STARCIRCLELIKE_LIST);
				mContext.sendBroadcast(intent);
			}
		});
		
		Intent intent = new Intent();
		intent.setAction(MainActivity.ACTION_STARCIRCLE_LIST);
		mContext.sendBroadcast(intent);
	}
	
	public void putLikeData(String cid, String number) {
		for (int i = 0; i < infoList.size(); i++) {
			if (infoList.get(i).getCid().equals(cid)) {
				infoList.get(i).setC_like(number);
			}
		}
		adapter.setData(infoList);
		adapter.notifyDataSetChanged();
	}
	
	public void putData(ArrayList<StarCircleData> list) {
		this.infoList = list;
		adapter.setData(infoList);
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		default:
			break;
		}
	}

}
